
***** Measure deterrence effects with synthetic controls

local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {

	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes

	** Keep only defendant 
	use op_ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"

	merge 1:1 dt using "synth_fit_`dft'.dta"

	local treatment_period = ``dft'Treat'


	** Produce difference in series
	gen deterrence = fit1 - out if dt >= `treatment_period'

	** Time discount the difference at 10% 
	local r = 1.1^(1/12)

	gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
	replace deterrence_disc = . if dt-`treatment_period' >= 60
	count if !mi(deterrence_disc)
	assert(r(N)==60)

	*** Compute total
	preserve
	collapse (sum) deterrence_disc
	qui summarize deterrence_disc
	local deterrence_`dft' = r(min)
	restore

	*** Truncate graph at 5 years 
	drop if dt  - `treatment_period' >= 60
	summarize dt 
	local minperiod = r(min)
	local maxperiod = r(max)

	** Find max Y 
	summarize fit1
	local ymax = 1.1*r(max)

	format dt %tmMon_YY
	twoway (scatter pmt_amt dt, mcolor(gs5)) (line out fit1 dt, lwidth(thick thick) lcolor(navy maroon)), xline(`treatment_period', lcolor(maroon)) xline(``dft'Settle', lcolor(black)) legend(order(2 "Defendant: `dft'" 3 "Synthetic Control") width(100)) xscale(range(`minperiod' `maxperiod')) xlabel(504(24)600 ``dft'Treat' ``dft'Settle') xtitle("Month") ytitle("$/Month") graphregion(color(white)) ylabel(#3) yscale(range(0 `ymax'))

	graph save "graphs/`dft'Overlay.gph", replace
}


cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes

log using deterrence.log, replace
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
local total = 0 
foreach dft of local dftlist {
	disp "`dft' Deterrence:"
	disp `deterrence_`dft''
	local total = `total' + `deterrence_`dft''
}
disp `total'
log close 





**** Multipanel
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/graphs

graph combine StJoOverlay.gph WheatonOverlay.gph ElCentroOverlay.gph, rows(1) xsize(10) ysize(2.5) l1title("$/Month") saving("OtherDftTop_OP.gph", replace)
graph combine  OverlookOverlay.gph MortonPlantOverlay.gph ShandsOverlay.gph, rows(1) xsize(10) ysize(2.5) l1title("$/Month") saving("OtherDftBottom_OP.gph", replace)
graph combine OtherDftTop_OP.gph OtherDftBottom_OP.gph, rows(2) xsize(10) ysize(5)
	
	* Remove $/Month from each panel
	** Make sure names are visible
	** Make sure y-axis and x-axis isn't cramped
	** Make red synthetic control dashed 

graph save "MultiPanel_otherdft_op.gph", replace
graph export "MultiPanel_otherdft_op.pdf", replace




